package cn.hy.mygame.entity;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import cn.hy.mygame.global.Config;
import cn.hy.mygame.model.BaseModel;
import cn.hy.mygame.model.TouchAble;

public class Sun extends BaseModel implements TouchAble {
	private int locationX;
	private int locationY;
	private boolean isAlive;
	private Rect touchArea;
	private SunState state;
	private long birthTime;
	private float xSpeed;
	private float ySpeed;
	
	public enum SunState {
		SHOW, COLLECT
	}
	
	public Sun (int locationX, int locationY) {
		this.locationX = locationX;
		this.locationY = locationY;
		this.state = SunState.SHOW;
		isAlive = true;
		touchArea = new Rect(locationX, locationY, locationX + Config.sun.getWidth(), locationY + Config.sun.getHeight());
		birthTime = System.currentTimeMillis();
	}
	
	@Override
	public void drawSelf(Canvas canvas, Paint paint) {
		// TODO Auto-generated method stub
		if (isAlive) {
			if (state == SunState.COLLECT) {
				locationX -= xSpeed;
				locationY -= ySpeed;
			}
			canvas.drawBitmap(Config.sun, locationX, locationY, paint);
			if (System.currentTimeMillis() - birthTime > 5000 && state == SunState.SHOW) {
				isAlive = false;
			} else if (state == SunState.COLLECT) {
				if (locationY < 0) {
					isAlive = false;
				}
			}
		}
	}

	@Override
	public boolean onTouch(MotionEvent event) {
		// TODO Auto-generated method stub
		if (state == SunState.SHOW) {
			int x = (int) event.getX();
			int y = (int) event.getY();
			if (touchArea.contains(x, y)) {
				xSpeed = (locationX - Config.sunCollectX) / 10f;
				ySpeed = (locationY - Config.sunCollectY) / 10f;
				state = SunState.COLLECT;
			}
		}
		return false;
	}
	
	public int getLocationX() {
		return locationX;
	}

	public void setLocationX(int locationX) {
		this.locationX = locationX;
	}

	public int getLocationY() {
		return locationY;
	}

	public void setLocationY(int locationY) {
		this.locationY = locationY;
	}

	public boolean isAlive() {
		return isAlive;
	}

	public void setAlive(boolean isAlive) {
		this.isAlive = isAlive;
	}
}
